package com.kedll.library.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

import com.kedll.library.widget.TabBarTextView;

import java.util.List;

/**
 * Created by slj on 2015/1/9.
 */
public class TabBarAdapter extends ArrayAdapter<String> {

    protected int widthPx;
    private int colorRes;

    public TabBarAdapter(Context context, List<String> objects, int widthPx) {
        super(context, 0, objects);
        this.widthPx = widthPx;
    }


    public void setColorRes(int colorRes) {
        this.colorRes = colorRes;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TabBarTextView view = new TabBarTextView(getContext());
        view.setText(getItem(position));
        view.setGravity(Gravity.CENTER);
        view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        if (colorRes != 0) {
            view.setTextColor(getContext().getResources().getColorStateList(colorRes));
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthPx, LinearLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        view.setTag(getItem(position));
        return view;
    }
}
